package com.jason.cloud.engine.share.driver.pack.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jason.cloud.engine.share.driver.pack.bean.PackBean;
import com.jason.cloud.engine.share.driver.pack.service.PackService;
import com.jason.cloud.engine.share.driver.role.bean.RoleBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.json.JsonArray;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

@Controller
@RequestMapping("/pack")
public class PackController {

    private static final Logger logger = LoggerFactory.getLogger(PackController.class);

    @Resource
    private PackService packService;

    /**
     * 加载
     * @param id
     * @return
     */
    @RequestMapping(value = "/query/{id}", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public @ResponseBody JSONObject queryPackById(@PathVariable String id) {
        PackBean packBean = this.packService.queryPackById(id);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name", packBean.getName());
        return jsonObject;
    }

    /**
     * 添加
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public @ResponseBody JSONObject insertPack(@RequestBody JSONObject json) {
        //取值
        String name = json.getString("name");
        int projects = json.getInteger("projects");
        int users = json.getInteger("users");
        int price = json.getInteger("price");
        String unit = json.getString("unit");
        boolean status = json.getBooleanValue("status");

        //保存
        JSONObject jsonObject = new JSONObject();
        PackBean packBean = new PackBean();
        packBean.setName(name);
        packBean.setProjects(projects);
        packBean.setUsers(users);
        packBean.setPrice(price);
        packBean.setUnit(unit);
        packBean.setDefault(false);
        packBean.setStatus(status);
        packBean.setCreateDate(new Date());
        try {
            this.packService.insertPack(packBean);
            jsonObject.put("status", "200");
            jsonObject.put("msg", "资费添加成功");
        } catch (Exception e) {
            jsonObject.put("status", "201");
            jsonObject.put("msg", "资费添加失败");
        }
        return jsonObject;
    }

    /**
     * 列举
     * @return
     */
    @RequestMapping(value = "/all", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public @ResponseBody JSONObject getPackByLimit(@RequestParam int page, @RequestParam int limit) {

        //加载所有
        List<PackBean> packList = this.packService.getPackByLimit((page - 1 ) * limit, limit);
        int count = this.packService.getPackCount();

        //返回值
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        jsonObject.put("msg", "");
        jsonObject.put("count", count);

        //遍历数据
        JSONArray jsonArrayPack = new JSONArray();
        PackBean packBean = null;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for(Iterator itr = packList.iterator(); itr.hasNext();) {
            packBean = (PackBean)itr.next();
            JSONObject json = new JSONObject();
            json.put("id", packBean.getId());
            json.put("name", packBean.getName());
            json.put("projects", packBean.getProjects());
            json.put("users", packBean.getUsers());
            json.put("price", packBean.getPrice());
            json.put("unit", packBean.getUnit());
            json.put("isDefault", packBean.isDefault()?"默认":"--");
            json.put("status", packBean.isStatus()?"可用":"--");
            json.put("createDate", simpleDateFormat.format(packBean.getCreateDate()));
            jsonArrayPack.add(json);
        }
        jsonObject.put("data", jsonArrayPack);
        return jsonObject;
    }


    /**
     * 废弃
     * @param paramStr
     * @return
     */
    @RequestMapping(value = "/disabled", method = RequestMethod.PUT, produces = "application/json;charset=UTF-8")
    public @ResponseBody JSONObject disabledPack(@RequestBody String paramStr) {
        //取值
        JSONArray jsonArray = JSON.parseArray(paramStr);

        //处理
        JSONObject jsonObject = null;
        for(int i = 0; i < jsonArray.size(); i++) {
            jsonObject = jsonArray.getJSONObject(i);
            String id = jsonObject.getString("id").trim();

            //更新
            this.packService.updatePackStatusDisabledById(id);
        }

        //返回值
        jsonObject = new JSONObject();
        jsonObject.put("status", 200);
        jsonObject.put("msg", "资费废弃成功");

        return jsonObject;
    }

    /**
     * 可用
     * @param paramStr
     * @return
     */
    @RequestMapping(value = "/enabled", method = RequestMethod.PUT, produces = "application/json;charset=UTF-8")
    public @ResponseBody JSONObject enabledPack(@RequestBody String paramStr) {
        //取值
        JSONArray jsonArray = JSON.parseArray(paramStr);

        //处理
        JSONObject jsonObject = null;
        for(int i = 0; i < jsonArray.size(); i++) {
            jsonObject = jsonArray.getJSONObject(i);
            String id = jsonObject.getString("id").trim();

            //更新
            this.packService.updatePackStatusEnabledById(id);
        }

        //返回值
        jsonObject = new JSONObject();
        jsonObject.put("status", 200);
        jsonObject.put("msg", "资费启用成功");

        return jsonObject;
    }

    /**
     * 默认
     * @param id
     * @return
     */
    @RequestMapping(value = "/default", method = RequestMethod.PUT, produces = "application/json;charset=UTF-8")
    public @ResponseBody JSONObject defaultPack(@RequestBody String id) {

        //返回值
        JSONObject jsonObject = new JSONObject();

        PackBean packBean = this.packService.queryPackById(id.trim());
        if (packBean.isStatus()) {
            //将之前默认的资费取消
            packBean = this.packService.queryDefaultPackByIsDefault();
            this.packService.updatePackNotDefaultById(packBean.getId().trim());

            //设置新的默认
            this.packService.updatePackIsDefaultById(id);

            jsonObject.put("status", 200);
            jsonObject.put("msg", "默认资费设置成功");
        } else {
            jsonObject.put("status", 201);
            jsonObject.put("msg", "资费已废弃、不能设置成默认");
        }
        return jsonObject;
    }

    /**
     * 加载可用资费
     * @return
     */
    @RequestMapping(value = "/loadPack", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public @ResponseBody JSONObject getPackByEnabled() {

        //加载所有
        List<PackBean> packBeanList = this.packService.getPackByEnabled();

        //返回值
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 200);
        jsonObject.put("msg", "");

        //遍历数据
        JSONArray jsonArrayRole = new JSONArray();
        PackBean packBean = null;
        for(Iterator itr = packBeanList.iterator(); itr.hasNext();) {
            packBean = (PackBean)itr.next();
            JSONObject json = new JSONObject();
            json.put("id", packBean.getId());
            json.put("name", packBean.getName());
            jsonArrayRole.add(json);
        }
        jsonObject.put("data", jsonArrayRole);
        return jsonObject;
    }
}
